package com.shf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shf.entity.Role;
import com.shf.entity.Users;
import com.shf.mapper.RoleMapper;
import com.shf.mapper.UserMapper;
import com.shf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户实现业务类
 * @author 叶枫
 */
@Service("us")
public class UserServiceImpl  implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;
    @Override
    public void saveUser(Users users) {
        //调用持久层方法

      int i =  userMapper.saveUser(users);

    }

    @Override
    public PageInfo<Users> UserFindAll(int page, int size) {
        //调用持久层方法
        PageHelper.startPage(page,size);
       List<Users>  usersList=userMapper.UserFindAll();
        PageInfo<Users> pageInfo = new PageInfo<Users>(usersList);

        return  pageInfo;
    }

    @Override
    public Users UserDetail(Integer id) {
        //调用持久层方法

      return   userMapper.userDetail(id);
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @Override
    public Users findUserById(Integer id) {
        return userMapper.findUserById(id);
    }

    @Override
    public List<Role> findAllRole() {
        return  roleMapper.findAllRole();
    }

    @Override
    public void addRoleToUser(String userId,String[] ids) {
        for (String id : ids) {
            roleMapper.addRoleToUser(userId,id);
        }

    }

    @Override
    public void deleteUser(String id) {
        //删除用户
        userMapper.deleteUser(id);
        //删除用户的权限
        roleMapper.deleteRoleUser(id);
    }


}
